import portalRoutes from '../../../portal/src/router/routes.js'
//import commonNavRoutes from '../../../common-nav/src/router/routes.js'
import authRoutes from '../../../auth/src/router/routes.js'
import baseRoutes from '../../../base/src/router/routes.js'
import attendRoutes from '../../../attend/src/router/routes.js'
import workflowRoutes from '../../../workflow/src/router/routes.js'
import wxRoutes from '../../../wx/src/router/routes.js'
import compositeRoutes from '../../../composite/src/router/routes.js'
import sequenceRoutes from '../../../sequence/src/router/routes.js'
import crmRoutes from '../../../crm/src/router/routes.js'
import houseRoutes from '../../../house/src/router/routes.js'
// 给路由的path添加前端，并复制一份原始路由
let pathWrapPrefix = (array,prefix)=>{
    // 复制一个新路由数组，目的是兼容模块路由的路径问题，
    // 什么意思呢，比如：base项目如果单独启动路由上有一个基础路由配置，这样在路由切换的时候会自动拼接，但单机运行会丢失这个base，所以为了兼容两种情况，复制一份路由且路由的路径写死，相当于带base的一份，不带base的一份
    let temp = []
    array.forEach((item,index)=>{
        let tempItem = {}
        for(let key in item){
            tempItem[key] = item[key]
        }
        temp.push(tempItem)
        item.path = prefix+ item.path
    })
    array.push.apply(array,temp)
}
// 给单个路由添加子路由
let addChildren = (item,children) => {
    let parentChildren = item.children
    if(!parentChildren){
        parentChildren = []
    }
    parentChildren.push.apply(parentChildren,children)
    item.children = parentChildren
}
// /auth 来自的auth项目的路由基础路径配置
pathWrapPrefix(authRoutes,'/auth')
// /base 来自的base项目的路由基础路径配置
pathWrapPrefix(baseRoutes,'/base')
pathWrapPrefix(attendRoutes,'/attend')
pathWrapPrefix(workflowRoutes,'/workflow')
pathWrapPrefix(wxRoutes,'/wx')
pathWrapPrefix(compositeRoutes,'/composite')
pathWrapPrefix(sequenceRoutes,'/sequence')
pathWrapPrefix(crmRoutes,'/crm')
pathWrapPrefix(houseRoutes,'/house')
//  添加一个空的/common-nav导航，以解决切换不同路由刷新的问题，登录成功后会导航到该页面
const emptyRoutes =  [
        {
            path: '/common-nav',
            component: () => import('@/views/Empty.vue')
        },
    {
        path: '/',
        component: () => import('@/views/Empty.vue')
    }
]
const routes = []
// commonNavRoutes为portalRoutes的子路由
// authRoutes和baseRoutes 路由为commonNavRoutes的子路由
portalRoutes.forEach(portalItem => {
    // 去除到 /*的路由，因为该路由为加载微前端的路由
    if(portalItem.path == '/*'){
        portalItem.component = () => import('../../../common-nav/src/App.vue')
        addChildren(portalItem,authRoutes)
        addChildren(portalItem,baseRoutes)
        addChildren(portalItem,attendRoutes)
        addChildren(portalItem,workflowRoutes)
        addChildren(portalItem,wxRoutes)
        addChildren(portalItem,compositeRoutes)
        addChildren(portalItem,sequenceRoutes)
        addChildren(portalItem,crmRoutes)
        addChildren(portalItem,houseRoutes)
        // 下面这个放到最后
        addChildren(portalItem,emptyRoutes)
    }
    routes.push(portalItem)
})
export default routes